package tw.edu.tku.im.maze;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MazeCell {
   int x,y;
   
   //-------------------------------------------------------
   public MazeCell(int x, int y) {
      this.x = x;
      this.y = y;
   }
   
   //-------------------------------------------------------
   public static MazeCell parse(JSONObject jsonObj) throws JSONException {
      JSONArray cellJSONObj = jsonObj.getJSONArray("cell");
      return new MazeCell(cellJSONObj.getInt(0), cellJSONObj.getInt(1));
   }
   
   //-------------------------------------------------------
   public int getX() { 
      return this.x;
   }
   public void setX(int x) {
      this.x = x;
   }
   
   //-------------------------------------------------------
   public int getY() { 
      return this.y;
   }
   public void setY(int y) {
      this.y = y;
   }
   
   //-------------------------------------------------------
   public String toString() {
      StringBuilder ans = new StringBuilder("{ \"cell\" : [");
      ans.append(x).append(",").append(y).append("] }");
      return ans.toString();
   }
   
   //-------------------------------------------------------
   public String toJSONString() throws JSONException {
      JSONObject jsonObj = new JSONObject();
      JSONArray cell = new JSONArray();
      cell.put(this.x);
      cell.put(this.y);
      jsonObj.put("cell", cell);
      return jsonObj.toString();
   }
}

